package com.ifp.opengate.boot.utils;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * @desc AES 加密工具类
 * @author 灭霸 2018-11-05
 */
public class AESUtil {

    /**
     * 加密算法key
     */
    private static final String KEY_ALGORITHM = "AES";

    /**
     * 默认的加密算法
     */
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 字符编码
     */
    private static final String CHARSET = "utf-8";

    /**
     * AES 密钥长度 （128位）
     */
    private static final int KEY_LENGTH = 128;

    /**
     * AES 加密
     *
     * @param content 待加密内容
     * @param password 加密密码
     * @return 返回Base64转码后的加密数据
     * @throws Exception
     */
    public static String encrypt(String content, String password) throws Exception {
        /*// 创建密码器
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        byte[] byteContent = content.getBytes(CHARSET);
        // 初始化为加密模式的密码器
        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
        // 加密
        byte[] result = cipher.doFinal(byteContent);
        // 通过Base64转码返回
        return Base64.encodeBase64String(result);*/
        // return DesUtils.encryptDES(content,password);
        return AESUtils.encrypt(content, password);
    }

    /**
     * AES 解密
     *
     * @param content
     * @param password
     * @return 明文
     * @throws Exception
     * 
     */
    public static String decrypt(String content, String password) throws Exception {
        /*// 实例化
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        // 使用密钥初始化，设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
        // 执行操作
        byte[] result = cipher.doFinal(Base64.decodeBase64(content));
        String encryptString =new String(result, CHARSET);
        return encryptString;*/

        // DesUtils.decryptDES(content,password);
        return AESUtils.decrypt(content, password);
    }

    /**
     * 生成加密密钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    private static SecretKeySpec getSecretKey(final String key) throws NoSuchAlgorithmException {
        /*if (null == key || key.length() == 0) {
        	throw new NullPointerException("key not is null");
        }
        SecretKeySpec key2 = null;
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(key.getBytes());
        try {
        	KeyGenerator kgen = KeyGenerator.getInstance("AES");
        	kgen.init(128, random);
        	SecretKey secretKey = kgen.generateKey();
        	byte[] enCodeFormat = secretKey.getEncoded();
        	key2 = new SecretKeySpec(enCodeFormat, "AES");
        } catch (NoSuchAlgorithmException ex) {
        	throw new NoSuchAlgorithmException();
        }
        return key2;*/

        // 返回生成指定算法密钥生成器的 KeyGenerator 对象
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        // AES 密钥长度为
        kg.init(KEY_LENGTH, new SecureRandom(key.getBytes()));
        // 生成一个密钥
        SecretKey secretKey = kg.generateKey();
        // 转换为AES专用密钥
        return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
    }
}